package com.lorenzogao.latte_core.net

import android.content.Context
import com.lorenzogao.latte_core.net.callback.*
import com.lorenzogao.latte_core.net.downlaod.DownloadHandler
import com.lorenzogao.latte_core.ui.LatteLoader
import com.lorenzogao.latte_core.ui.LoaderStyle
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import java.io.File
import java.util.*

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2020-01-08 11:26
 *  description:
 */

class RestClient {

    private val PARAMS = RestCreator.PARAMS

    private val REQUEST: IRequest?

    private val SUCCESS: ISuccess?

    private val FAILURE: IFailure?

    private val ERROR: IError?

    private val BODY: RequestBody?

    private val URL: String

    private var LOADER_STYLE: LoaderStyle?
    private val context: Context?

    private val DOWNLOAD_DIR:String?

    private val EXTENSION:String?

    private val NAME:String?

    val FILE: File?

    constructor(
        params: MutableMap<String, Any>,
        request: IRequest?,
        success: ISuccess?,
        failure: IFailure?,
        error: IError?,
        body: RequestBody?,
        url: String,
        loaderStyle: LoaderStyle?,
        context: Context?,
        file: File?,
        dowload_dir:String?,
        extension:String?,
        name:String?
    ) {
        this.PARAMS.putAll(params)
        this.REQUEST = request
        this.SUCCESS = success
        this.FAILURE = failure
        this.ERROR = error
        this.BODY = body
        this.URL = url
        this.LOADER_STYLE = loaderStyle
        this.context = context
        this.FILE = file
        this.DOWNLOAD_DIR=dowload_dir
        this.EXTENSION=extension
        this.NAME=name
    }


    companion object {
        fun builder(): RestClientBuilder {
            return RestClientBuilder()
        }
    }


    fun request(mehtod: HttpMethod) {
        val service = RestCreator.getRestService
        var call: Call<String>?

        LOADER_STYLE?.let {
            context?.let { it1 -> LatteLoader.showLoading(it1) }
        }

        REQUEST?.onRequestStart()
        call = when (mehtod) {
            HttpMethod.GET -> service.get(URL, converter())
            HttpMethod.POST -> service.post(URL, converter())
            HttpMethod.POST_RAW -> BODY?.let { service.postRaw(URL, it) }
            HttpMethod.PUT -> service.put(URL, converter())
            HttpMethod.PUT_RAW -> BODY?.let { service.putRaw(URL, it) }
            HttpMethod.DELETE -> service.delete(URL, converter())
            HttpMethod.UPLOAD -> {
                var requestBody =
                    RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()), FILE)
                val body = MultipartBody.Part.createFormData("file", FILE?.name, requestBody)
                service.upload(URL, body)
            }

        }
        call?.enqueue(getRequestCallback())

    }


    private fun getRequestCallback() =
        RequestCallbacks(REQUEST, SUCCESS, FAILURE, ERROR, LOADER_STYLE)


    private fun converter(): WeakHashMap<String, String> {
        val map = WeakHashMap<String, String>()
        for (param in PARAMS) {
            map[param.key] = "${param.value}"
        }
        return map

    }

     fun get() {
        request(HttpMethod.GET)
    }

    fun post() {
        if (BODY == null) {
            request(HttpMethod.POST)
        } else {
            require(!PARAMS.isEmpty().not()) { "params must be null" }
            request(HttpMethod.POST_RAW)
        }

    }


     fun put() {
        if (BODY == null) {
            request(HttpMethod.PUT)
        } else {
            require(!PARAMS.isEmpty().not()) { "params must be null" }
            request(HttpMethod.PUT_RAW)
        }
    }

    fun delete() {
        request(HttpMethod.DELETE)
    }

    fun downlaod(){
     DownloadHandler(REQUEST,SUCCESS,FAILURE,ERROR,URL,DOWNLOAD_DIR,EXTENSION,NAME)
    }


}